<?php

/**
 * 优惠管理
 * @author Ice <iceinto@mallog.com.cn>
 */
class LimitedModel extends BaseModel
{

    protected $_type = 'limited';

    /**
     * 获取当前领取活动
     * @return Array 优惠详细
     * @author Ice <iceinto@mallog.com.cn>
     */
    public function getNows()
    {
        //TODO 分城市获取
        $map = array();
        $dayTime = time();
        $map['start_time'] = array('lt', $dayTime);
        $map['end_time'] = array('gt', $dayTime);
        $limiteds = $this->field('id,title,image,start_time,end_time,count')->where($map)->order('start_time DESC')->select();
        $Date = new Date($dayTime);
        foreach ($limiteds as $key => $limited) {
            if ($limited['model'] == 2) {
                $push = $this->getMallPush($limited['id']);
            } else {
                $push = $this->getPush($limited['id']);
            }
            $limiteds[$key]['push'] = $push;
            $limiteds[$key]['surplus'] = $limited['count'] - $push;
            $limiteds[$key]['image_url'] = $this->getManagedUrl($limited['image'], $this->_type);
            //获取结束时间
            $limiteds[$key]['diff_deay'] = intval($Date->dateDiff(intval($limited['end_time'])));
        }
        return $limiteds;
    }

    /**
     * 获取热门活动
     * @return Array 优惠详细
     * @author Ice <iceinto@mallog.com.cn>
     */
    public function getHot()
    {
        //TODO 分城市获取
        $map = array();
        $dayTime = time();
        $map['start_time'] = array('lt', $dayTime);
        $map['end_time'] = array('gt', $dayTime);
        $map['bid'] = array('neq', 0);
        $limiteds = $this->field('id,title,bid')
            ->where($map)
            ->limit('0,6')
            ->order('start_time DESC')
            ->select();
        if ($limiteds) {
            foreach ($limiteds as $key => $limited) {
                $ad = $this->getAdMFB(0, 0, $limited['bid']);
                $ad['brand']['image_url'] = D('Brand')->getBrandImage($ad['brand']['small_image']);
                $limiteds[$key]['brand'] = $ad['brand'];
            }
        }
        return $limiteds;
    }

    /**
     * 根据优惠标识获取优惠已经发送数量
     * @param Int $id 优惠标识
     * @return Int 领取数量
     * @author Ice <iceinto@mallog.com.cn>
     */
    public function getPush($lid)
    {
        $map['lid'] = $lid;
        $push = M('LimitedUsers')->field('count(1) as push')->where($map)->group('lid')->find();
        return intval($push['push']);
    }

    /**
     * 根据商场优惠标识获取优惠已经发送数量
     * @param Int $id 优惠标识
     * @return Int 领取数量
     * @author Ice <iceinto@mallog.com.cn>
     */
    public function getMallPush($lid)
    {
        $map['lid'] = $lid;
        $map['status'] = 2;
        $push = M('LimitedUsers')->field('count(1) as push')->where($map)->group('lid')->find();
        return intval($push['push']);
    }

    /**
     * 根据用户获取领取信息
     * @param int $uid 用户标示
     * @param string  $limit
     * @return array
     * @author Ice <iceinto@mallog.com.cn>
     */
    public function getUserJoin($uid, $limit = null)
    {
        $map['m_limited_users.uid'] = $uid;
        return $this->field('m_limited_users.id,lid,title,start_time,end_time,receive_start_time,receive_end_time,image,mid,fid,bid,count,send_count')
            ->where($map)
            ->limit($limit)
            ->order('m_limited.id DESC')
            ->join('INNER JOIN m_limited_users ON m_limited.id = m_limited_users.lid')
            ->select();
    }

    /**
     * 获取用户领取的优惠数量
     * @param $uid
     * @return mixed
     * @author Ice <iceinto@mallog.com.cn>
     */
    public function getUserJoinCount($uid)
    {
        $map['m_limited_users.uid'] = $uid;
        return $this->where($map)
            ->join('INNER JOIN m_limited_users ON m_limited.id = m_limited_users.lid')
            ->count();
    }

    /**
     * 获取优惠所在地
     * @param array $limited 优惠信息
     * @author Ice <iceinto@mallog.com.cn>
     */
    public function getAddress(&$limited)
    {
        if (!$limited) {
            return false;
        }
        $mfb = $this->getAdMFB($limited['mid'], $limited['fid'], $limited['bid']);
        $limited = array_merge($limited, $mfb);
    }
}